<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addClick(1, '0')"
            >添加权限</el-button
          >
        </template>
      </page-tools>
      <!-- 表格数据 -->
      <el-table border :data="list" row-key="id">
        <el-table-column label="名称" prop="name"> </el-table-column>
        <el-table-column label="标识" align="center" prop="code">
        </el-table-column>
        <el-table-column label="描述" align="center" prop="description">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="{ row }">
            <!-- 添加按钮只在访问层级显示, 只有type为1才是第一层 -->
            <el-button
              type="text"
              v-if="row.type === 1"
              @click="addClick(2, row.id)"
              >添加</el-button
            >
            <el-button type="text" @click="editClick(row.id)">编辑</el-button>
            <el-button type="text" @click="delClick(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="showTitle" :visible="showdialog" @close="btnCancel">
      <el-form
        label-width="120px"
        :model="formData"
        :rules="rules"
        ref="formData"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input
            style="width:90%"
            v-model="formData.name"
            prop="name"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input
            style="width:90%"
            v-model="formData.code"
            prop="code"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input style="width:90%" v-model="formData.description"></el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  addPermission,
  updatePermission,
  delPermission,
  getPermissionDetail
} from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  data () {
    return {
      list: [],
      showdialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle () {
      return this.formData.id ? '编辑权限' : '添加权限'
    }
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList () {
      let res = await getPermissionList()
      console.log(res)
      this.list = tranListToTreeData(res.data, '0')
    },
    // 删除权限点
    delClick (id) {
      // console.log(id)
      this.$confirm('确定要删除该权限点吗?')
        .then(async () => {
          await delPermission(id)
          this.$message.success('删除成功')
          // 重新拉去数据
          this.getPermissionList()
        })
        .catch(error => {
          console.log(error)
        })
    },
    addClick (type, pid) {
      // 访问权限的type=1,按钮操作的权限type=2
      this.showdialog = true
      // 记录当前点击的类型和父标识
      this.formData.type = type
      this.formData.pid = pid
    },
    btnOk () {
      this.$refs.formData
        .validate()
        .then(() => {
          if (this.formData.id) {
            // 有id是编辑
            return updatePermission(this.formData)
          }
          return addPermission(this.formData)
        })
        .then(() => {
          this.$message.success('操作成功')
          // 重新拉取数据
          this.getPermissionList()
          this.showdialog = false
        })
    },
    // 取消按钮关闭弹层
    btnCancel () {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.formData.resetFields() // 移出校验
      this.showdialog = false
    },
    // 编辑权限
    async editClick (id) {
      let res = await getPermissionDetail(id)
      // console.log(res)
      this.formData = res.data
      this.showdialog = true
    }
  }
}
</script>

<style></style>
